/**
 * 独立角色
 */
const aloneActor = {};

class Game_ActorBox {
    constructor() {
        this._data = new Map();
        this._battleSymbol = [];
        this.setupStartingMembers();
    }

    createActor(symbol, actorId) {
        const actor = new Game_Actor(actorId);
        actor._symbol = symbol;
        if (this._data.has(symbol)) {
            const random = Math.randomInt(10000);
            this.createActor(`${symbol}${random}`, actorId);
        }
        this._data.set(symbol, actor);
    }

    setupStartingMembers() {
        for (const actorId of $dataSystem.partyMembers) {
            const symbol = $dataActors[actorId].name;
            this.createActor(symbol, actorId);
        }
    }

    allMembers() {
        return [...this._data.values()];
    }

    battleMembers() {
        const members = [];
        for (const symbol of this._battleSymbol) {
            if (symbol) {
                const actor = this._data.get(symbol);
                members.push(actor);
            }
        }
        return members;
    }

    actor(symbol) {
        return this._data.get(symbol);
    }
}

Game_Actor.prototype.symbol = function () {
    return this._symbol;
};

Game_Party.prototype.allMembers = function () {
    return $gameActorBox.allMembers();
};

Game_Party.prototype.battleMembers = function () {
    return $gameActorBox.battleMembers();
};

aloneActor.setSubject = Game_Action.prototype.setSubject
Game_Action.prototype.setSubject = function (subject) {
    this._subjectActorSymbol = subject.symbol();
    aloneActor.setSubject.call(this, subject);
}
aloneActor.subject = Game_Action.prototype.subject;
Game_Action.prototype.subject = function () {
    const symbol = this._subjectActorSymbol;
    if (symbol) return $gameActorBox.actor(symbol);
    return aloneActor.subject.call(this);
}

aloneActor.createGameObjects = DataManager.createGameObjects;
DataManager.createGameObjects = function () {
    aloneActor.createGameObjects.call(DataManager);
    $gameActorBox = new Game_ActorBox();
}

aloneActor.makeSaveContents = DataManager.makeSaveContents;
DataManager.makeSaveContents = function () {
    const contents = aloneActor.makeSaveContents.call(this);
    contents.actorBox = $gameActorBox;
    return contents;
}

aloneActor.extractSaveContents = DataManager.extractSaveContents;
DataManager.extractSaveContents = function (contents) {
    aloneActor.extractSaveContents.call(this);
    $gameActorBox = contents.actorBox;
}

aloneActor.ceshi = function () {
    for (i = 0; i < 10; i++) {
        const symbol = `ceshi${i}`
        $gameActorBox.createActor(symbol, 1);
    }
}
